package com.msblearn.C_007;

public class T003_Wait_Notify {

    public static void main(String[] args) {
        String[] abc = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
        Object o = new Object();

        new Thread(() -> {
            synchronized (o) {
                for (int i = 0; i < 10; i++) {
                    System.out.print(i + 1);
                    try {
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //必须nofity。无法结束
                o.notify();
            }
        }).start();
        new Thread(() -> {
            synchronized (o) {
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < 10; i++) {
                    try {
                        System.out.print(abc[i]);
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();
            }
        }).start();

    }
}
